package com.ag.admin.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.ag.admin.service.impl.UserServiceImpl;
import com.ag.bean.TbContact;
import com.ag.bean.TbUser;
import com.ag.bean.TbUserState;
import com.ag.common.AjaxNotify;
import com.ag.common.BaseAction;
import com.ag.common.IPermissionService;

@Scope("prototype")
@Controller("userAction")
public class UserAction extends BaseAction implements SessionAware{

	private static final long serialVersionUID = 1L;
	private static final Logger logger = Logger.getLogger(UserAction.class);

	/**
	 * 调用权限服务逻辑组件
	 */
	@Resource(name = "PermissionServiceImpl")
	private IPermissionService pms;
	/**
	 * 提供Map<String, Object>类型的Session变量，用来保存Struts2注入的Session上下文
	 */
	private Map<String, Object> sessionMap;
	@Override
	public void setSession(Map<String, Object> session) {
		this.sessionMap = session;
	}
	
	@Resource(name = "userServiceImpl")
	private UserServiceImpl userService;
	
	private String selRoles;
	private String selDepts;
	//private String leadDepts;
	private TbUser user =new TbUser();
	private TbContact contact = new TbContact();
	private List<TbUser> users;
	private List<TbContact> contacts;
	private List<TbUserState> userStates;
	private String ajaxRst;
	
	/**
	 * 注册用户，selRoles的形式是String或",,1001,1002"
	 * @return
	 */
	public void save() {
		this.userService.save(user,selRoles,selDepts);
		this.outJsonString("true");
	}
	
	public void delete(){
		Boolean b = this.userService.delete(user.getUId());
		logger.info("message"+b);
		this.outJsonString(b.toString());
	}
	/**
	 * 用户管理中修改用户
	 * @return
	 */
	public String editUser() {
		user = this.userService.findById(user.getUId());
		return "editUser";
	}
	/**
	 * 个人用户修改
	 * @return
	 */
	public String editOne() {
		TbUser u = null;
		try {
			u = pms.getCurrentUser(sessionMap);
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		try {
			user = this.userService.findById(u.getUId());
		}catch (Exception e ){
			e.printStackTrace();
			return ERROR;
		}
		return "editOne";
	}
	/**
	 * 用户管理中更新用户
	 * @return
	 */
	public void update() {
		this.userService.update(user,selRoles,selDepts);
		this.outJsonString("true");
	}
	
	/**
	 * hmk 
	 */
	public void updateExpertUser() {
		this.userService.updateExpertUser(user, selRoles, selDepts);
		this.outJsonString("true");
	}
	/**
	 * 个人用户管理中更新用户
	 * @return
	 */
	public void updateOne() {
		Boolean b = this.userService.updateOne(user);
		this.outJsonString(b.toString());
	}
	/**
	 * 用户管理中根据用户ID查找用户详情
	 * @return
	 */
	public String findUserByUId() {
		user = this.userService.findById(user.getUId());
		return "findUserByUId";
	}
	
	public String findUserInfoByUId() {
		TbUser u = null;
		try {
			u = pms.getCurrentUser(sessionMap);
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		if(u!=null){
			user = this.userService.findById(u.getUId());
		}else{
			return ERROR;
		}
		return "findUserInfoByUId";
	}
	
	/**
	 * 查找所有用户
	 * @return
	 */
	public String findAllUser() {
		users = this.userService.findAllUser();
		return "findAllUser";
	}
	
	/**
	 * 查找所有的用户状态
	 * @return
	 */
	public String findAllUserState(){
		logger.info("do find state");
		userStates = this.userService.fndAllUserState();
		logger.info("userStates="+userStates.size());
		return "findAllUserState";
	}
	
	/**
	 * 注册时检查登录名是否存在
	 */
	public void checkUId(){
		Boolean b = this.userService.checkUId(user.getUId());
		this.outJsonString(b.toString());
	}
	
	/**
	 * 注册时检查真实姓名的通讯录是否存在
	 */
	public String findContactByUName(){
		logger.info("UName="+user.getUName());
		contacts = this.userService.findContactByUName(user.getUName());
		if(contacts.size()==0){
			this.ajaxRst = AjaxNotify.NULLVALUE;
		}else{
			this.ajaxRst = AjaxNotify.SUCCESS;
		}
		return "findContactByUName";
	}
	
	public TbUser getUser() {
		return user;
	}

	public void setUser(TbUser user) {
		this.user = user;
	}

	public List<TbUser> getUsers() {
		return users;
	}

	public void setUsers(List<TbUser> users) {
		this.users = users;
	}

	public TbContact getContact() {
		return contact;
	}

	public void setContact(TbContact contact) {
		this.contact = contact;
	}

	public String getSelRoles() {
		return selRoles;
	}

	public void setSelRoles(String selRoles) {
		this.selRoles = selRoles;
	}

	public String getAjaxRst() {
		return ajaxRst;
	}

	public void setAjaxRst(String ajaxRst) {
		this.ajaxRst = ajaxRst;
	}

	public List<TbContact> getContacts() {
		return contacts;
	}

	public void setContacts(List<TbContact> contacts) {
		this.contacts = contacts;
	}

	public List<TbUserState> getUserStates() {
		return userStates;
	}

	public void setUserStates(List<TbUserState> userStates) {
		this.userStates = userStates;
	}

	public String getSelDepts() {
		return selDepts;
	}

	public void setSelDepts(String selDepts) {
		this.selDepts = selDepts;
	}

}
